import { test, expect } from "@playwright/test";

test("通过JWT访问受限页面", async ({ page, context }) => {
  // 1. 设置JWT作为Cookie
  await context.addCookies([
    {
      name: "jwt", // 根据后端要求可能是 'token'/'access_token' 等
      value:
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MCwicGFzc3dvcmQiOiIxMjM0NTYiLCJyZWFsTmFtZSI6IlZiZW4iLCJyb2xlcyI6WyJzdXBlciJdLCJ1c2VybmFtZSI6InZiZW4iLCJpYXQiOjE3NTM5NDg0MjAsImV4cCI6MTc1NjU0MDQyMH0.sy2gN5n7MXUQ6NeyMOd2V3krjoF6gGAu1spIjwmDyGs",
      domain: "localhost",
      path: "/",
      httpOnly: true, // 通常JWT需要httpOnly
      secure: false, // 本地开发用false，生产环境必须true
    },
  ]);
  // 设置全局请求头
  await context.setExtraHTTPHeaders({
    Authorization:
      "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTQ1NDk4MjMsImlhdCI6MTc1NDQ2MzQyMywidXNlcklkIjoxfQ.TETMedfQU58eUO7KTZd6DX0c86E9TL_ltkTjuZvAT8g",
  });
  // 2. 导航到目标页面
  await page.goto("http://localhost:5777/#/auth/login", {
    waitUntil: "networkidle",
    timeout: 20000000,
  });
  await page.waitForLoadState("networkidle"); // 等待所有请求静止
  // 本地存储数据
  await page.evaluate(() =>
    localStorage.setItem(
      "vben-web-ele-5.5.2-dev-core-access",
      JSON.stringify({
        accessToken:
          "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTQ1NDk4MjMsImlhdCI6MTc1NDQ2MzQyMywidXNlcklkIjoxfQ.TETMedfQU58eUO7KTZd6DX0c86E9TL_ltkTjuZvAT8g",
        refreshToken:
          "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTk2NDc0MjMsImlhdCI6MTc1NDQ2MzQyMywidXNlcklkIjoxfQ.JxBO1Kme6OF9FLcPGNH5-fJm7ht_r7kA1dvkonhBo9c",
        accessCodes: [],
      })
    )
  );
  // 等待2秒
  await page.waitForTimeout(2000);
  // 调转另一个地址
  await page.goto("http://localhost:5777/#/customerbind", {
    waitUntil: "networkidle",
    timeout: 20000000,
  });
  // 等待 50 秒 结束程序
  await page.waitForTimeout(50000);
});

// test("666", async ({ page, context }) => {
//   await page.goto("http://localhost:5777/#/customerchat", {
//     waitUntil: "networkidle",
//     timeout: 20000,
//   });
// });
